package com.htandroid.htandroid.views;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.htandroid.htandroid.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by caih on 2018/7/5.
 */
public class MySelectedDialogFragment extends DialogFragment {

    EasyPickView epv_keep;
    TextView tv_sure;
    private SureListener sureListener;

    public static MySelectedDialogFragment newInstance(int layoutId, List<String> data, SureListener sureListener) {
        MySelectedDialogFragment f = new MySelectedDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("layout", layoutId);
        bundle.putStringArrayList("data", (ArrayList<String>) data);
        f.setArguments(bundle);
        f.sureListener = sureListener;
        return f;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.BOTTOM;
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(attributes);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getArguments().getInt("layout"), container, false);
        //设置窗口以对话框样式显示
//		setStyle(DialogFragment.STYLE_NO_TITLE, R.style.dialog);
        //设置对话框背景色，否则有虚框
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置对话框弹出动画，从底部滑入，从底部滑出
        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialog_animation;
        getDialog().setCancelable(true);
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().getWindow().setGravity(Gravity.BOTTOM);

        epv_keep = (EasyPickView) view.findViewById(R.id.epv_keep);
        tv_sure = (TextView) view.findViewById(R.id.tv_sure);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        epv_keep.setDataList(getArguments().getStringArrayList("data"));
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sureListener != null) {
                    sureListener.sure(epv_keep.getValue());
                }
                dismissAllowingStateLoss();
            }
        });
    }

    public interface SureListener {
        void sure(String str);
    }
}
